package com.jl.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jl.commons.base.BaseController;
import com.jl.commons.constant.Constants;
import com.jl.commons.constant.ErrorCode;
import com.jl.commons.constant.ErrorMessage;
import com.jl.commons.result.PageDataInfo;
import com.jl.commons.result.ResultData;
import com.jl.commons.utils.BASE64;
import com.jl.model.ProductionLine;
import com.jl.model.in.ProductionLineDevice;
import com.jl.service.IDeviceService;
import com.jl.service.IFaultInfoService;
import com.jl.service.IProductionLineService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 生产线表 前端控制器
 * </p>
 *
 * @author JiangJian
 * @since 2019-03-06
 */
@Controller
@RequestMapping("/productionLine")
public class ProductionLineController extends BaseController {

    @Autowired
    private IProductionLineService productionLineService;

    @Autowired
    private IDeviceService deviceService;
    @Autowired
    private IFaultInfoService faultInfoService;

    @GetMapping("/productionLineDataGrid")
    @RequiresPermissions("/productionLine/productionLineDataGrid")
    public String productionLineList(){
        return "productionLine_list";
    }


    @GetMapping("/add")
    @RequiresPermissions("/productionLine/add")
    public String addProductionLinePage(ModelMap mp, Long id){
        mp.put("id",id);
        return "productionLine_add";
    }

    /**
     * 一定是add
     * @param json
     * @return
     */
    @PostMapping("/add")
    @RequiresPermissions("/productionLine/add")
    @ResponseBody
    public ResultData addProductionLine(String json){
        String value = BASE64.decode(json);
        if(null == value){
            return fail(ErrorCode.BASE64_DECODER_ERROR, ErrorMessage.BASE64_DECODER_ERROR);
        }
        ProductionLine productionLine = JSON.parseObject(value,ProductionLine.class);

            //判断名称是否重复，不能拥有重复名称
            if(productionLineService.isProductionLineNameUsed(productionLine.getName())){
                //被占用
                return fail(ErrorCode.PRODUCTION_NAME_USED, ErrorMessage.PRODUCTION_NAME_USED);
            }
            productionLine.setCreateId(getUserId());
            productionLine.setGmtCreate(new Date());

        productionLineService.save(productionLine);
        return success(productionLine);
    }


    @GetMapping("/edit")
    @RequiresPermissions("/productionLine/edit")
    public String editProductionLinePage(ModelMap mp, Long id){
        mp.put("id",id);
        return "productionLine_add";
    }

    /**
     * 一定是add
     * @param json
     * @return
     */
    @PostMapping("/edit")
    @RequiresPermissions("/productionLine/edit")
    @ResponseBody
    public ResultData editProductionLine(String json){
        String value = BASE64.decode(json);
        if(null == value){
            return fail(ErrorCode.BASE64_DECODER_ERROR, ErrorMessage.BASE64_DECODER_ERROR);
        }
        ProductionLine productionLine = JSON.parseObject(value,ProductionLine.class);

        //判断编号是不是自己的，如果是自己的则表示可以修改
        boolean canUse = productionLineService.isProductionLineNameCanUse(productionLine.getName(),productionLine.getId());
        if(!canUse){
            return fail(ErrorCode.DEVICE_NUMBER_USED,ErrorMessage.DEVICE_NUMBER_USED);
        }
        productionLine.setGmtModified(new Date());
        productionLine.setModifyId(getUserId());

        productionLineService.updateById(productionLine);
        return success(productionLine);
    }

    @PostMapping("/delete")
    @RequiresPermissions("/productionLine/delete")
    @ResponseBody
    public ResultData deletesProductionLine(String json){
        String value = BASE64.decode(json);
        if(null == value){
            return fail(ErrorCode.BASE64_DECODER_ERROR, ErrorMessage.BASE64_DECODER_ERROR);
        }
        List<Long> ids = JSONArray.parseArray(value,Long.class);
        boolean b = productionLineService.removeByIds(ids);
        if(b){
            return success();
        }
        return fail();
    }

    /**
     * 一定是dataGrid
     * @param current
     * @param pageSize
     * @param searchCol
     * @param searchValue
     * @return
     */
    @PostMapping("/dataGrid")
    @RequiresPermissions("/productionLine/dataGrid")
    @ResponseBody
    public ResultData productionLineDataGrid(Long current,Long pageSize,String searchCol,String searchValue){
        if(current == null || pageSize == null){
            return fail(ErrorCode.PAGE_PARAM_ERROR, ErrorMessage.PAGE_PARAM_ERROR);
        }
        QueryWrapper<ProductionLine> queryWrapper = new QueryWrapper<>();
        //目前的查询只有单表
        if(StringUtils.isNotBlank(searchValue) && StringUtils.isNotBlank(searchCol)){
            if(searchCol.equals(Constants.ID)){
                queryWrapper.eq("t.id",searchValue);
            }else{
                queryWrapper.like("t."+searchCol,searchValue);
            }
        }
        PageDataInfo<Map<String,Object>> pageDataInfo =new PageDataInfo<>();
        pageDataInfo.setCurrent(current);
        pageDataInfo.setSize(pageSize);

        productionLineService.selectProductionLineDataGrid(pageDataInfo,queryWrapper);
        return success(pageDataInfo);
    }

    /**
     * 跳转到生产线的故障信息列表页面
     * @param modelMap
     * @param lineId
     * @return
     */
    @GetMapping("/lineFaultInfo")
    @RequiresPermissions("/productionLine/lineFaultInfo")
    public String lineFaultInfo(ModelMap modelMap,Long lineId){
        modelMap.put("lineId",lineId);
        return "lineDeviceFaultInfo_list";
    }

    /**
     * 获取生产线上的设备故障信息
     */
    @PostMapping("/lineFaultInfo")
    @RequiresPermissions("/productionLine/lineFaultInfo")
    @ResponseBody
    public ResultData lineFaultInfo(Long current,Long pageSize,Long lineId){
        PageDataInfo<Map<String,Object>> pageDataInfo =new PageDataInfo<>();
        pageDataInfo.setCurrent(current);
        pageDataInfo.setSize(pageSize);
        faultInfoService.getFaultInfoByLineId(pageDataInfo,lineId);
        return success(pageDataInfo);
    }
    /**
     * 查询该生产线的设备情况
     * @param lineId
     * @return
     */
    @PostMapping("/lineDeviceInfo")
    @RequiresPermissions("/productionLine/lineFaultInfo")
    @ResponseBody
    public ResultData lineDeviceInfo(Long lineId){
        List<ProductionLineDevice> lineDeviceList = deviceService.getLineDeviceRunInfo(lineId);
        PageDataInfo<ProductionLineDevice> pageDataInfo =new PageDataInfo<>();
        pageDataInfo.setCurrent(Constants.FIRST_PAGE);
        pageDataInfo.setSize(lineDeviceList.size());
        pageDataInfo.setRecords(lineDeviceList);
        return success(pageDataInfo);
    }

    /**
     * 查询本月该生产线故障统计信息
     * @param lineId
     * @return
     */
    @PostMapping("/lineStatistics")
    @RequiresPermissions("/productionLine/lineFaultInfo")
    @ResponseBody
    public ResultData lineDamageStatisticsInfo(Long lineId,Integer type){
        List<Map<String,Object>> dataInfo = productionLineService.getLineFaultStatistics(lineId,type);
        return success(dataInfo);
    }


}
